package com.stox.client.ui.core.model;

import java.util.ArrayList;
import java.util.List;

import com.stox.client.ui.core.model.intf.Converter;
import com.stox.client.ui.core.model.intf.Navigable;

public class NavigableAdapter<T, V> implements Navigable<V> {

	private final Navigable<T> navigable;
	private final Converter<T,V> converter;
	private final Class<V> clazz;
	
	public NavigableAdapter(Navigable<T> navigable,
			Converter<T,V> converter, Class<V> type) {
		this.converter = converter;
		this.navigable = navigable;
		this.clazz = type;
	}
	
	@Override
	public Object getTarget() {
		return navigable.getTarget();
	}

	@Override
	public V first() {
		return converter.from(navigable.first());
	}

	@Override
	public V previous(V from) {
		return converter.from(navigable.previous(converter.to(from)));
	}

	@Override
	public V current() {
		return converter.from(navigable.current());
	}

	@Override
	public List<V> currentAllSelection() {
		List<T> list = navigable.currentAllSelection();
		if(null == list){
			return null;
		}
		List<V> result = new ArrayList<V>();
		for(T object : list){
			result.add(converter.from(object));
		}
		return result;
	}

	@Override
	public V next(V from) {
		return converter.from(navigable.next(converter.to(from)));
	}

	@Override
	public V last() {
		return converter.from(navigable.last());
	}

	@Override
	public void scrollClearAndSelect(V entity) {
		navigable.scrollClearAndSelect(converter.to(entity));
	}

	@Override
	public Class<V> getNavigableEntityClass() {
		return clazz;
	}

}
